package com.woniu108.valueAdded.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu108.carService.commos.dto.ScoreDetailLogDto;
import com.woniu108.carService.commos.dto.TicketDto;
import com.woniu108.valueAdded.mapper.ScoreDetailLogMapper;
import com.woniu108.valueAdded.model.ScoreDetailLog;
import com.woniu108.valueAdded.param.ScoreRecordsTimeParam;
import com.woniu108.valueAdded.service.ScoreDetailLogService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 积分明细表 服务实现类
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@Service
public class ScoreDetailLogServiceImpl extends ServiceImpl<ScoreDetailLogMapper, ScoreDetailLog> implements ScoreDetailLogService {

    @Resource
    private ScoreDetailLogMapper scoreDetailLogMapper;
    @Override
    public Page<ScoreDetailLogDto> userScoreRecords(ScoreRecordsTimeParam param) {
        QueryWrapper<ScoreDetailLog> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",param.getUserId());
        List<ScoreDetailLog> logList = scoreDetailLogMapper.selectList(wrapper);
        List<ScoreDetailLogDto> dtos = BeanUtil.copyToList(logList, ScoreDetailLogDto.class);
        //分页
        long total = dtos.size();
        long pages=total%param.getPageSize()==0?total/param.getPageSize():total/param.getPageSize()+1;
        Page<ScoreDetailLogDto> page=new Page<ScoreDetailLogDto>();
        List<ScoreDetailLogDto> list =new ArrayList<>();
        int i=0;
        for (ScoreDetailLogDto scoreDetailLogDto : dtos) {
            if(i<param.getPageNum()*param.getPageSize().intValue()&&i>=(param.getPageNum()-1)*param.getPageSize().intValue()){
                list.add(scoreDetailLogDto);
            }
            i=i+1;
        }
        page.setCurrent(param.getPageNum());
        page.setSize(param.getPageSize());
        page.setRecords(list);
        page.setTotal(total);
        page.setPages(pages);
        return page;
    }
}
